//
//  RcvAddressAddViewController.m
//  DuBai
//
//  Created by darktm on 14-3-18.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "RcvAddressAddViewController.h"

#import "Addresses.h"

#import "TaskWithUpdateAddress.h"
#import "TaskWithAddAddress.h"
#import "TaskWithSetDefaultAddress.h"

@interface RcvAddressAddViewController ()<WYHttpTaskDelegate>

@end

@implementation RcvAddressAddViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.completeBtn.layer.cornerRadius = 13.0f;
    
    self.txtName.layer.cornerRadius = 4.0f;
    self.txtAddress.layer.cornerRadius = 4.0f;
    self.txtPhone.layer.cornerRadius = 4.0f;
    
    if(self.address){
        self.txtPhone.text = self.address.telephone;
        self.txtAddress.text = self.address.address;
        self.txtName.text = self.address.consignee;
        self.txtEmail.text = self.address.email;
    }
    
}



#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithUpdateAddress class]]){
        [self.navigationController popViewControllerAnimated:YES];
    }else if([obj isKindOfClass:[TaskWithSetDefaultAddress class]]){
        
    }else if([obj isKindOfClass:[TaskWithAddAddress class]]){
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    if(delegate && [delegate respondsToSelector:@selector(rcvAddressDidEnd)]){
        [delegate rcvAddressDidEnd];
    }
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.txtEmail resignFirstResponder];
    [self.txtAddress resignFirstResponder];
    [self.txtName resignFirstResponder];
    [self.txtPhone resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)on_btn_add:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    Addresses *a = [[Addresses alloc] init];
    a.address = self.txtAddress.text;
    a.consignee = self.txtName.text;
    a.telephone = self.txtPhone.text;
    a.email = self.txtEmail.text;
    
    if(self.address){//update
        
        a.id = self.address.id;
        
        TaskWithUpdateAddress *update = [[TaskWithUpdateAddress alloc] init];
        update.address = a;
        update.delegate = self;
        [update start];
    }else{//add
        
        TaskWithAddAddress *update = [[TaskWithAddAddress alloc] init];
        update.address = a;
        update.delegate = self;
        [update start];
    }
}
@end
